Optimizing my Ford-Fulkerson implementation for sparse graphs.
[and.git] / 12317 - Document Compression / 12317.3.cpp
blobdf8f0f43ebf36c20dbb130e82dfd4eac4f507dfd
1 // Equipo Poncho, Carriel y Ruana
2 // Accepted on UVa
3 using namespace std;
4 #include <algorithm>
5 #include <iostream>
6 #include <iterator>
7 #include <sstream>
8 #include <fstream>
9 #include <cassert>
10 #include <climits>
11 #include <cstdlib>
12 #include <cstring>
13 #include <string>
14 #include <cstdio>
15 #include <vector>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <map>
21 #include <set>
23 template <class T> string toStr(const T &x)
24 { stringstream s; s << x; return s.str(); }
25 template <class T> int toInt(const T &x)
26 { stringstream s; s << x; int r; s >> r; return r; }
28 #define For(i, a, b) for (int i=(a); i<(b); ++i)
29 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
30 #define D(x) cout << #x " = " << (x) << endl;
32 const double EPS = 1e-9;
34 int cmp(double x, double y = 0, double tol = EPS) {
35 return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
38 const int MAXBASES = 105, oo = 1 << 28;
39 int bases[MAXBASES];
41 int B, D;
42 int dp[1 << 16];
44 int docSize;
46 void binary(int x){
47 for (int i = 0; i < 16; ++i){
48 printf("%d", !!(x & (1 << i)));
52 void precompute(){
53 for (int mask = 0; mask < (1 << 16); ++mask) {
54 dp[mask] = oo;
56 dp[0] = 0;
58 for (int i = 0; i < B; ++i) {
59 for (int mask = 0; mask < (1 << 16); ++mask) {
60 if (dp[mask] >= oo) continue;
61 int base = bases[i];
62 dp[mask | base] = min(dp[mask | base], dp[mask] + 1);
67 int main(){
68 while (scanf("%d %d", &B, &D) == 2) {
69 if (B == 0 and D == 0) break;
70 for (int i = 0; i < B; ++i) {
71 bases[i] = 0;
73 int k; scanf("%d", &k);
74 while (k--) {
75 int x; scanf("%d", &x); x--;
76 bases[i] |= (1 << x);
79 precompute();
80 for (int j = 0; j < D; ++j) {
81 int doc = 0;
82 int k; scanf("%d", &k);
83 while (k--) {
84 int x; scanf("%d", &x); x--;
85 doc |= (1 << x);
88 int ans = dp[doc];
89 if (ans >= oo) ans = 0;
90 printf("%d", ans);
92 if (j + 1 < D) printf(" ");
94 puts("");
96 return 0;